//验证密码必须有字母、数字、特殊字符中的两种
function verifyPassword(password){
    var pw_pattern = /^(?![A-Za-z]+$)(?![A-Z0-9]+$)(?![a-z0-9]+$)(?![a-z\W_!@#$%^&*`~()-+=]+$)(?![A-Z\W_!@#$%^&*`~()-+=+$)(?![0-9\W_!@#$%^&*`~()-+=]+$)[a-zA-Z0-9\W_!@#$%^&*`~()-+=]{8,16}$/

    return pw_pattern.test(password)
}

//判断是否为手机号的正则表达式
function phoneFun(phones){
    var myreg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
    if (!myreg.test(phones)) {
        console.log('手机号格式不正确')
        return false;
    } else {
        console.log('手机号格式正确')
        return true;
    }
}

function setCookie(cname,cvalue,exdays){
    var d = new Date();
    d.setTime(d.getTime()+(exdays*24*60*60*1000));
    var expires = "expires="+d.toGMTString();
    document.cookie = cname+"="+cvalue+"; "+expires+";domain=.wncinema.com;path=/";
}
function getCookie(cname){
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i=0; i<ca.length; i++) {
        var c = ca[i].trim();
        if (c.indexOf(name)==0) { return c.substring(name.length,c.length); }
    }
    return "";
}



//设置token
const TokenKey = 'Web-Admin-Token'

function getToken() {
  return getCookie(TokenKey)
}

function setToken(token) {
  return setCookie(TokenKey, token, 1000)
}

function removeToken() {
  return setCookie(TokenKey, null, 0)
}

//获取url中的参数
function GetQueryString(name)
{
    let reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    let searchurl =decodeURI(window.location.search)
    let r = searchurl.substr(1).match(reg);//search,查询？后面的参数，并匹配正则
    if(r!=null)return  unescape(r[2]); return null;
}

//设置axios拦截request和response
axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
axios.defaults.timeout=10000
axios.defaults.baseURL = 'http://api.wncinema.com/api/'
// 创建axios实例
// const http = axios.create({
  // axios中请求配置有baseURL选项，表示请求URL公共部分
  // baseURL: 'http://api.wncinema.com/api/',
  // 超时
  // timeout: 10000
// sss})
// request拦截器
axios.interceptors.request.use(config => {
  // 是否需要设置 token
  const isToken = (config.headers || {}).isToken === false
  if (getToken() && !isToken) {
    config.headers['Authorization'] = 'Bearer ' + getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
  }
  // get请求映射params参数
  if (config.method === 'get' && config.params) {
    let url = config.url + '?';
    for (const propName of Object.keys(config.params)) {
      const value = config.params[propName];
      var part = encodeURIComponent(propName) + "=";
      if (value !== null && typeof(value) !== "undefined") {
        if (typeof value === 'object') {
          for (const key of Object.keys(value)) {
            let params = propName + '[' + key + ']';
            var subPart = encodeURIComponent(params) + "=";
            url += subPart + encodeURIComponent(value[key]) + "&";
          }
        } else {
          url += part + encodeURIComponent(value) + "&";
        }
      }
    }
    url = url.slice(0, -1);
    config.params = {};
    config.url = url;
  }
  return config
}, error => {
    // alert(error)
    console.log(error)
    Promise.reject(error)
})

// 响应拦截器
axios.interceptors.response.use(res => {
    // 未设置状态码则默认成功状态
    const code = res.data.code || 200;
    // 获取错误信息
    const msg = res.data.msg

	console.log('response-->',res)
    if (code === 401) {
		alert('请登录')
		location.href = '../login.vue';

    } else if (code === 500) {
      alert('后台服务异常')
      return Promise.reject(""+msg)
    } else if (code !== 200) {
      alert(msg)
      return Promise.reject('error')
    } else {
        // alert(res.data)
      return res.data
    }
  },
  error => {
    console.log('err--->' + error)
    alert('commonjs响应拦截error'+ error)
    return Promise.reject(error)
  }
)
